package com.chentf.beanpost;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @author chentengfei
 * @desc ...
 * @date 2021-01-14 22:54:21
 */
public class MyBeanPostProcessor implements BeanPostProcessor {
    /*
        前置方法一般不做处理,但是Spring创建好的对象会先传个该方法,所以该方法需要返回bean对象给容器,
        以便after方法接收Bean对象
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        //此处的判断很有必要,因为BeanPostProcessor会对配置文件中的所有对象进行加工,此处可以过滤不需要加工的对象
        if (bean instanceof Categroy) {
            Categroy categroy = (Categroy) bean;
            categroy.setName("sunwukong");
        }
        return bean;
    }
}
